home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / pc / Shout3Ddemo / Shout3d_runtime / codebase / custom_nodes / SpriteEffect.java < prev    next >
Text File  |  2000-09-05  |  2KB  |  72 lines

  1. /**    
  2.     Company:        Eyematic Interfaces
  3.     Project:        Shout3D 2.0 Sample Code
  4.     Class:            SpriteEffect
  5.     Date:            March 23, 2000
  6.     Description:    A simple example of a PostRenderEffect that displays a sprite over the scene
  7.     (C) Copyright Eyematic Interfaces, Inc. - 1997-2000 - All rights reserved
  8.  */
  9.  
  10. package custom_nodes;
  11.  
  12. import shout3d.core.*;
  13. import shout3d.*;
  14. import java.awt.*;
  15. import java.applet.*;
  16. import java.net.*;
  17.  
  18. /**
  19.  * SpriteEffect
  20.  * 
  21.  * @author Jim Stewartson
  22.  * @author Paul Isaacs
  23.  */
  24.  
  25. public class SpriteEffect extends PostRenderEffect implements FieldObserver{
  26.     
  27.     public final StringField     imageUrl            = new StringField(        this, "imageUrl",        Field.ANY,                "");
  28.     final public IntField         xPosition            = new IntField(            this, "xPosition",        Field.NON_NEGATIVE_INT,    0);
  29.     final public IntField         yPosition            = new IntField(            this, "yPosition",        Field.NON_NEGATIVE_INT,    0);
  30.     final public BooleanField     centered            = new BooleanField(        this, "centered",        Field.ANY,    false);
  31.                         
  32.  
  33.     /**
  34.      * Constructs a default SpriteEffect node.
  35.      * NOTE: imageUrl is relative to directory in which model file is contained.
  36.      */
  37.     public SpriteEffect(){
  38.         imageUrl.addFieldObserver(this, null);
  39.     }
  40.  
  41.     public void onFieldChange(Field f, Object userData){
  42.         if (f == imageUrl) getImage();
  43.     }
  44.     
  45.     Image image;
  46.     protected void getImage(){
  47.         if (getViewer().getComponent() instanceof Applet){
  48.             try{
  49.                 // getCurrentBaseURL() returns the directory in which the model file is located.
  50.                 URL myURL = new URL(getViewer().getResourceListener().getCurrentBaseURL(), imageUrl.getValue());
  51.                 Applet applet = (Applet)getViewer().getComponent();
  52.                 image = applet.getImage(myURL);
  53.             }
  54.             catch(Exception e){
  55.                 e.printStackTrace();
  56.             }
  57.         }    
  58.     }
  59.  
  60.     public void filter(Graphics offScreenGraphics, int surface_pixel_bits[], float z_buffer[], int deviceWidth, int deviceHeight){
  61.         if (image != null){
  62.             int x = xPosition.getValue();
  63.             int y = yPosition.getValue();
  64.             if (centered.getValue()){
  65.                 x -= image.getWidth(null)/2;
  66.                 y -= image.getHeight(null)/2;
  67.             }
  68.             offScreenGraphics.drawImage(image, x, y, null);
  69.         }
  70.     }
  71.     
  72. }